package j.framework.tags.easyui;

import j.framework.tags.form.FormTagWriter;
import j.util.StringUtils;

import javax.servlet.jsp.JspException;

/**
 * easyui版的form tag
 * User: jason
 * Date: 2015/1/16 14:59
 */
public class FormTag extends j.framework.tags.form.FormTag {
    @Override
    public int doEndTag() throws JspException {
        FormTagWriter writer = getWriter();
        writer.closeTag("form");
        StringBuilder sb = new StringBuilder();
        sb.append("<script>var submitting=false;var formId='").append(getFormId()).append("';");
        sb.append("$('#").append(getFormId()).append("').form({url:'");
        sb.append(getAction()).append("',onSubmit:function(){submitting = true;$.messager.progress({text:'正在保存数据…'});");
        if(StringUtils.isNotEmpty(getOnSubmit())){
            sb.append(getOnSubmit());
        }
        sb.append("}"); //end append onSubmit
        sb.append(",success:function(data){submitting = false;$.messager.progress('close');/*将data转换为json格式*/var jsonData = Http.parseJSON(data);doProcessFormResponse(jsonData);}");
        sb.append("});");
        sb.append("</script>");
        writer.writeSomething(sb.toString());
        writer.flush();
        writer = null;
        return EVAL_PAGE;
    }
}
